package com.frame.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.PageContext;

import com.app.Const;
import com.pioneer.app.dev.osuser.user.AdminService;
import com.pioneer.app.dev.osuser.user.Userlist;
import com.pioneer.app.proview.RoleProcess;
import com.pioneer.app.proview.RoleProcessDAO;
import com.pioneer.app.proview.RoleUser;
import com.pioneer.app.proview.RoleUserDAO;
import com.pioneer.app.proview.UserProcess;
import com.pioneer.app.proview.UserProcessDAO;
import com.pioneer.app.proview.UserProcessId;

public class ProviewTest {

	public static boolean isPreview(PageContext pageContext,String processcode){
		Userlist user=(Userlist)pageContext.getSession().getAttribute(Const.SESSION_USER_OBJECT);
		if(AdminService.USER_NAME.equalsIgnoreCase(user.getUserName())){
			return true;
		}else{
			return  haveProview(user.getId(),processcode);
		}
	}
	public static boolean haveProview(Integer userId,String processId){
		boolean flg=false;
		try{
			/*
			List processArr=new ArrayList();
			RoleUserDAO ruDao=new RoleUserDAO();
			List roleList=ruDao.findByProperty("userId", userId);
			for(int i=0;i<roleList.size();i++){
				RoleUser ur=(RoleUser)roleList.get(i);
				RoleProcessDAO rpDao=new RoleProcessDAO();
				List proList=rpDao.findByProperty("roleId", ur.getRoleId());
				for(int j=0;j<proList.size();j++){
					RoleProcess rp=(RoleProcess)proList.get(j);					
					processArr.add(rp.getProcessId());
				}
			}
			for(int i=0;i<processArr.size();i++){
				if(processId.endsWith(processArr.get(i).toString())){
					flg=true;
				}
			}
			*/
			UserProcessDAO dao=new UserProcessDAO();
			UserProcessId upid=new UserProcessId();
			upid.setProcessId(processId);
			upid.setUserId(userId);
			UserProcess up=dao.findById(upid);
			if(null!=up)flg=true;
		}catch(Exception e){
			
		}
		return flg;
	}
}
